<?php

namespace Admin\Controller;
use Admin\AppConst\WebConst;
use Admin\Service\MenuService;
use Admin\Service\RoleService;
use Think\Controller;

class BaseController extends Controller {

    //参数获取
    public $getParams    = array();
    //定义头部信息
    protected $actionPath;          //当前controller.action名称
    protected $actionName;          //当前action名称
    protected $checkLoginAction;    //需要验证的控制器
    protected $siteTitle;
    protected $siteTitleName        = 'siteTitle';
    protected $urlController;
    protected $urlControllerName    = 'urlController';
    protected $actionTitle;
    protected $defaultModel;



    public function __construct()
    {
        parent::__construct();
        //用户token处理
        $this->actionName       = ACTION_NAME;
        $this->siteTitle        = C('siteConf')['appName'];
        $this->defaultModel     = C('DEFAULT_MODULE');
        $this->urlController    = '/'.$this->defaultModel.'/'.CONTROLLER_NAME;
        $this->actionPath       = $this->urlController.'/'.ACTION_NAME;


        $this->assign($this->siteTitleName, $this->siteTitle);
        $this->assign($this->urlControllerName , $this->urlController);

        $this->checkLoginAction = C('checkLoginActionList');

        $this->setParams();

        //初始化
        $this->init();

    }

    /* @function    setParams   设置参数
     * @date        2018-07-26
     * @author      zhangle
     */
    public function setParams(){
        if(is_array($_POST)){
            $this->getParams    = $_REQUEST;
        }else{
            $params = file_get_contents('php://input');
            if(!empty($params)){
                $this->getParams    = json_decode($params,WebConst::DEFAULT_TYPE_TRUE);
            }else{
                //参数错误
                $this->error('参数错误!');
            }
        }

        if(isset($this->getParams['page'])){
            $this->getParams['page']    = intval($this->getParams['page']);
            $this->getParams['page']    = ($this->getParams['page'] == 0) ? WebConst::DEFAULT_TYPE_PAGE : $this->getParams['page'];
        }else{
            $this->getParams['page']    = WebConst::DEFAULT_TYPE_PAGE;
        }
    }

    /* @function    init           //获取初始化信息
     * @date        2018-07-25
     * @author      zhangle
     */

    public function init(){
        //验证是否登录
        if(!in_array($this->actionPath, $this->checkLoginAction)){
            //验证用户是否登录
            $this->checkLogin();
            $this->checkRole();
            $menuIdList = session(WebConst::USER_SESSION_KEY)['menuList'];
            $menuList   = session(WebConst::MENU_SESSION_KEY);
            //获取菜单数据
            if(empty($menuList)){
                $menuList   = MenuService::get()->getList(WebConst::DEFAULT_TYPE_TRUE);
                //处理菜单权限
                foreach ($menuList as $id => $item) {
                    if(!in_array($item['id'], $menuIdList)){
                        unset($menuList[$id]);
                        continue;
                    }
                    foreach ($item['child'] as $key => $value) {
                        if(!in_array($value['id'], $menuIdList)){
                            unset($menuList[$id]['child'][$key]);
                            continue;
                        }
                        foreach ($value['child'] as $k => $val) {
                            if(!in_array($val['id'], $menuIdList)){
                                unset($menuList[$id]['child'][$key]['child'][$k]);
                                continue;
                            }
                        }
                    }
                }
                session(WebConst::MENU_SESSION_KEY, $menuList);
            }
            //处理是否展开菜单
            foreach ($menuList as $id => $item) {
                if($item['url'] == $this->actionPath){
                    $menuList[$id]['checked']   = WebConst::DEFAULT_TYPE_TRUE;
                }
                foreach ($item['child'] as $key => $value) {
                    if($value['url'] == $this->actionPath){
                        $menuList[$id]['checked']                   = WebConst::DEFAULT_TYPE_TRUE;
                        $menuList[$id]['child'][$key]['checked']    = WebConst::DEFAULT_TYPE_TRUE;

                        $menuList[$id]['show']                      = WebConst::DEFAULT_TYPE_TRUE;
                    }
                    foreach ($value['child'] as $k => $val) {
                        if($val['url'] == $this->actionPath){
                            $menuList[$id]['checked']                               = WebConst::DEFAULT_TYPE_TRUE;
                            $menuList[$id]['child'][$key]['checked']                = WebConst::DEFAULT_TYPE_TRUE;
                            $menuList[$id]['child'][$key]['child'][$k]['checked']   = WebConst::DEFAULT_TYPE_TRUE;

                            $menuList[$id]['show']                  = WebConst::DEFAULT_TYPE_TRUE;
                            $menuList[$id]['child'][$key]['show']   = WebConst::DEFAULT_TYPE_TRUE;

                        }
                    }
                }
            }
            $this->assign(WebConst::MENU_SESSION_KEY, $menuList);
        }
    }

    /* @function    验证用户是否已经登录
     * @date        2018-08-01
     * @author      zhangle
     */
    private function checkLogin(){
        if(is_null(session(WebConst::USER_SESSION_KEY))){
            redirect('/Admin/Account/index');
        }
    }

    /* @function    验证权限
     * @date        2018-08-04
     * @author      zhangle
     */
    private function checkRole(){
        //获取当前菜单id
        $menu = MenuService::get()->getMenuIdByUrl($this->actionPath);
        $result = RoleService::get()->checkRole($menu['id']);
        if($result == WebConst::DEFAULT_TYPE_FALSE){
            $this->error('对不起, 你没有权限!');
        }
    }
}
